<?php

namespace App\Admin\Controllers;

use App\Models\YiNew;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use App\Admin\Actions\NewsAction\NewsExamine;
use App\Admin\Actions\NewsAction\NewsExamineNone;
use App\Admin\Actions\NewsAction\NewsTranslate;
use App\Admin\Actions\NewsAction\NewsBriefCreate;
use App\Admin\Actions\NewsAction\NewsImageCheck;
use App\Admin\Actions\NewsAction\BatchNewsExamine;
use App\Admin\Actions\NewsAction\linkedinPost;
use App\Admin\Actions\NewsAction\tiktokPost;


class YiNewsController extends AdminController
{
    protected $title = '国内新闻';
    

    protected function grid()
    {
        $grid = new Grid(new YiNew());

		$grid->model()->select('id','title_cn','title_zn','images','is_examine','started_at','created_at','source','keywords','likes','collections',"mp_biz","views")->orderByDesc('id');

	    
	    
	    $grid->column('title_cn', '中文标题');
	    $grid->column('title_zn', '英文标题');
	   /* $grid->column('images', '封面')->display(function ($value) {
		       return img_path($value);
        })->lightbox(['width' => 50, 'height' => 50]);*/
         $grid->column('source', '来源')->display(function ($value) {
	       if ($value === 'weixin') {return _label('微信公众号', 'success');}
        });
         $grid->column('mp_biz', '公众号')->display(function ($value) {

	       return $this->mpWeixinCompany->nickname;
        });
        $grid->column('keywords', '关键词');
        $grid->column('views', '浏览');
        $grid->column('likes', '点赞');
        $grid->column('collections', '收藏');
        $grid->column('is_examine', '是否审核')->bool(); 
        $grid->column('started_at', '新闻发布时间')->sortable();
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });


	   	$this->gridActions($grid);
	   	$this->gridFilter($grid);
	   	
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->add(new NewsExamine());
			$actions->add(new NewsExamineNone());
			$actions->add(new NewsTranslate());
			/*$actions->add(new linkedinPost());
			$actions->add(new tiktokPost());*/
			
			/*$actions->add(new NewsBriefCreate());
			$actions->add(new NewsImageCheck());*/
			//$actions->add(new RefreshBlindBoxRate());
		});

		$grid->batchActions(function ($batch) {
		    $batch->add(new BatchNewsExamine());
		});
	}

	 protected function gridFilter(Grid $grid)
    {
	    $grid->filter(function (Grid\Filter $filter) {
		    $filter->expand();
	    	$filter->scope('is_examine', '已审核')->where('is_examine', true);
	    	$filter->scope('is_translate', '已翻译')->whereNotNull('title_zn');

	    	$filter->column(1/2, function ($filter) {
	    		$filter->like('title_cn','标题');
			    $filter->between('created_at','创建时间')->datetime();
			});

			$filter->column(1/2, function ($filter) {
	    		$filter->gt('views', "浏览量");
			});
	    	
	    });


    }


    protected function detail($id)
    {
        $show = new Show(YiNew::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new YiNew());

	   //	$form->select('blind_box_category_id','盲盒分类')->options(BlindBoxCategory::all()->pluck('name', 'id'));

	    $form->text('title_cn', '中文标题');
	     $form->text('title_zn', '英文标题');
 		$form->simditor('content_cn', '中文内容')->rules('required|string');
 		$form->simditor('content_zn', '英文内容');
 		$form->textarea('brief_zn', '英文摘要');
 		$form->simditor('brief', '摘要');
 		$form->text('url', '原文链接');
 		$form->image('images', '封面图片')
		    ->uniqueName()
		    ->creationRules('required');
		$form->display('programImages', '其他图片')->with(function ($value) {
	        // 假设 $value 是一个包含图片路径的数组

	        $html = '';
	        foreach ($value as $image) {
	            $html .= '<img src="'.img_path($image['url']).'" data-js="'.$image['url'].'" style="width: 100px; height: 100px; object-fit: cover; margin: 5px;" class="programImages">';
	        }
	        return $html;
	    });

	    

	    $this->formSaved($form);

        return $form;
    }


    protected function formSaved(Form $form)
	{

		$form->saved(function (Form $form) {
			;
			if($form->imageOther){
				$form->model()->update(['images'=>$form->imageOther]);
				//$form->images = $form->imageOther;
			}
			/*$originalStatus = $form->model()->getOriginal('refund_status');
			
			Log::debug("OrderRefund original status: {$originalStatus}");
			Log::debug("OrderRefund current status: {$form->refund_status}");
			
			$status = $originalStatus;
			
			if ($originalStatus === 'applied' and $form->refund_status === 'refused') {
				$status = 'refused';
			}
			if ($originalStatus === 'applied' and $form->refund_status === 'confirmed') {
				$status = 'confirmed';
			}*/
			
			//$form->refund_status = $status;
		});
	}
   
}
